package com.martin.lib_base.model

import android.os.CountDownTimer
import androidx.compose.runtime.mutableStateOf
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * @author：孟凡华
 * @date：2023/4/14
 * @desc：
 */
class CodeModel(
    private val text: String = "获取验证码"
) {

    // 验证码
    val code = mutableStateOf("")

    // 倒计时文案
    val sendText = mutableStateOf(text)

    // 是否可以点击
    val sendEnabled = mutableStateOf(true)

    // 倒计时
    private val countDownTimer = object : CountDownTimer(60000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            sendText.value = "${millisUntilFinished / 1000}秒后重发"
            sendEnabled.value = false
        }

        override fun onFinish() {
            sendText.value = text
            sendEnabled.value = true
        }
    }

    fun startCountDown() {
        countDownTimer.start()
        sendEnabled.value = false
    }

    fun cancelCountDown() {
        sendText.value = text
        countDownTimer.cancel()
        sendEnabled.value = true
    }

}